// 禁止在vuex中操作计时,频繁操作vuex会造成计时不准
import store from "@/store";
import guessHero from "@/api/modules/guessHero";
import user from "@/api/modules/user";
import {
  float2int
} from "@/utils";

const state = {
  games: [], // 游戏列表
  currentGame: 0, // 当前游戏索引
  type: "waiting", // waiting,timeing,lotterying,lotteryed
  typeData: null,
  score: 0, //  本场赢的积分
  scoreTotal: 0, // 该游戏所有场次赢的总积分
  preLotteryResault: null, // 上期开奖结果
  sunShine: null, // 当前阳光值信息
  lotteryResault: null, // 开奖结果已过滤为选择的选项(猜的选项的正确结果)
};

const actions = {
  // 获取所有游戏列表
  getGames(context) {
    return guessHero.games().then(_ => {
      context.commit('SET_GAMES', _.data.data.map(item => {
        item['allHeros'] = []; // 该游戏的所有英雄列表
        item['isSubmit'] = true; // 是否已经投注,默认已投注不允许投注
        item['submitData'] = null; // 投注数据
        return item;
      }));
      return _.data.data;
    });
  },
  // 获取阳光值
  getSunShine(context) {
    return user.getSunshineValue().then(_ => {
      context.commit('SET_SUN_SHINE', _.data.data);
      return _;
    });
  },
  // 进入房间
  enter(context, game) {
    getApp().globalData.socket.sendSocketMessage({
      c: "guessHero",
      a: "enter",
      data: {
        game
      },
      cb: "guessHero/ENTER"
    });
  },
  // 离开房间
  leave(context, game) {
      getApp().globalData.socket.sendSocketMessage({
        c: "guessHero",
        a: "leave",
        data: {
          game
        }
      });
  },
  // 提交预测
  postGuessHero(context, formData) {
    return guessHero.postGuessHero(formData).then(response => {
      // 设置投注数据
      let guessHero = store.state.guessHero;
      let games = JSON.parse(JSON.stringify(guessHero.games));
      games[guessHero.currentGame].submitData = formData;
      // 设置投注状态
      games[guessHero.currentGame].isSubmit = true;
      context.commit("SET_GAMES", games);
      return response;
    });
  }
};

const mutations = {
  // 设置游戏列表
  SET_GAMES(state, data) {
    state.games = data;
  },
  // 切换游戏索引
  SET_CURRENT_GAME(state, current) {
    // 先退出房间
    store.dispatch("guessHero/leave", state.games[state.currentGame].game);
    state.currentGame = current;
    // 判断是否需要获取所有英雄列表
    if (state.games[current].allHeros.length <= 0) {
      guessHero.getHeros(state.games[current].game, 1, 9999).then(
        response => {
          store.commit("guessHero/SET_CURRENT_GAME_HEROS", response.data.data.list || []);
        });
    }
    // 再进入房间
    store.dispatch("guessHero/enter", state.games[current].game);
  },
  // 设置当前游戏的所有英雄
  SET_CURRENT_GAME_HEROS(state, data) {
    state.games[state.currentGame]['allHeros'] = data;
  },
  // 进入
  ENTER(state, data) {
    uni.$emit("game_enter", data);
    state.typeData = data;
    //数据初始化
    state.score = 0;
    state.scoreTotal = 0;
    // 重置游戏数据
    let games = state.games;
    games[state.currentGame].isSubmit = false;
    games[state.currentGame].submitData = null;
    state.games = games;
    // 设置上一期开奖的英雄数据
    state.preLotteryResault = data.hero;
    // 如果当前状态不是倒计时那么就设置为等待中
    if (data.type !== "timeing") {
      state.type = "waiting";
    } else {
      state.type = "timeing";
      uni.$emit("game_timeing", data);
    }
  },
  // 倒计时
  // 该状态只能从lotteryed过渡进来
  timeing(state, data) {
    state.lotteryResault = null;
    state.typeData = data;
    state.type = "timeing";
    uni.$emit("betting_init");
    // 清空上一场的数据
    state.score = 0;
    // 重置游戏数据
    let games = state.games;
    games[state.currentGame].isSubmit = false;
    games[state.currentGame].submitData = null;
    state.games = games;
    uni.$emit("game_timeing", data);
  },
  // 开奖进行中
  // 该状态只能从timeing过渡进来
  lotterying(state, data) {
    // 此时肯定已经没法提交了
    state.typeData = data;
    state.type = "lotterying";
    uni.$emit("game_lotterying", data);
  },
  // 开奖完成
  // 该状态只能从lotterying过渡进来
  lotteryed(state, data) {
    store.dispatch("user/getUserInfo");
    state.typeData = data;
    state.type = "lotteryed";
    // 开奖结果
    let lotteryResault = {};
    // 当前结果当作上期结果传递下去
    state.preLotteryResault = data.hero;
    // 计算积分
    let games = state.games;
    let currentGameInfo = games[state.currentGame];
    let submitData = currentGameInfo.submitData,
      score = 0;
    if (submitData) {
      // 单双
      if (submitData['num'] == data.hero.num) {
        score += submitData['num_ap'] * currentGameInfo[`num${data.hero.num}_rate`];
      }
      if (submitData['num']) lotteryResault['num'] = data.hero.num;

      // 性别
      if (submitData['sex'] == data.hero.sex) {
        score += submitData['sex_ap'] * currentGameInfo[`sex${data.hero.sex}_rate`];
      }
      if (submitData['sex']) lotteryResault['sex'] = data.hero.sex;
      // 攻击方式
      if (submitData['remote'] == data.hero.remote) {
        score += submitData['remote_ap'] * currentGameInfo[
            `remote${data.hero.remote}_rate`];
      }
      if (submitData['remote']) lotteryResault['remote'] = data.hero.remote;
      // 有无魔法
      if (submitData['magic'] == data.hero.magic) {
        score += submitData['magic_ap'] * currentGameInfo[
            `magic${data.hero.magic}_rate`];
      }
      if (submitData['magic']) lotteryResault['magic'] = data.hero.magic;
    }
    state.lotteryResault = lotteryResault;
    // 本场获得的积分
    score = float2int(score)
    state.score = score;
    // 该游戏所有场次获得的积分
    state.scoreTotal += score;
    uni.$emit("game_lotteryed", data);
  },
  // 设置阳光值
  SET_SUN_SHINE(state, data) {
    state.sunShine = data;
  },
  SET_TYPE(state, type) {
    state.type = type;
  }
};


export default {
  namespaced: true,
  state,
  actions,
  mutations
};
